import { getSingerList } from '../../api/services/api'
export default ({
    namespaced: true,
    state: {
        currentType: {
            type: '-1',
            area: '-1',
            initial: 'A',
            limit: '28',
            currentPage: 1
        },
        loading: false,
        letterList: [],
        singerList: [],
    },
    mutations: {
        //加载动画
        changeLoading(state, val) {
            state.loading = !state.loading;
        },
        changeSingerList(state, val) {
            state.singerList = [...state.singerList, ...val];
        },
        //改变歌手类型
        changeCurrentType(state, val) {
            state.currentType.currentPage = 1;
            state.singerList = [];
            state.currentType.type = val;
        },
        //改变地区类型
        changeCurrentArea(state, val) {
            //每次触发滚动事件currentPage都会增加，导致切换类型时，加载的数据就不是第一页的，所以得重置
            state.currentType.currentPage = 1;
            state.singerList = [];
            state.currentType.area = val;
        },
        //改变字母
        changeInitial(state, val) {
            state.currentType.currentPage = 1;

            state.singerList = [];
            state.currentType.initial = val;
        },
        asciiCode(state, val) {
            let arr = [];
            for (let i = 65; i <= 90; i++) {
                arr.push(String.fromCharCode(i));
            }
            state.letterList = [...arr];
        },
        //滚动分页获取数据
        changeCurrentPage(state, val) {
            state.currentType.currentPage++;
        }
    },
    actions: {
        async getSingerList(context, val) {
            context.commit('changeLoading');
            const { type, area, initial, limit, currentPage } = context.state.currentType;
            const data = await getSingerList(type, area, initial, limit, currentPage);
            console.log(data);
            if (data.code == 200) {
                context.commit('changeSingerList', data.artists);
                context.commit('changeLoading');
                return data.artists;
            }
        },


    },

})
